package io.renren.common.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;


/**
 * @Author: zhangshulong
 * @Description:
 * @Date: Created in 13:45  2016/11/30
 */
public class MailUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(MailUtil.class);
   /* @Value("${mail.host}")*/
    private static String mailSmtpServer;
    private static String mailFrom;
    private static String mailPassword;
    private static MimeMessage message;
    private static Session session;
    @Autowired
    private static Environment env;

    private static Properties properties = new Properties();
    /**
     * 发送邮件
     */
    public static R sendEmail(String subject, String sendHtml, String receiveUser) {
        try {
            session = Session.getInstance(properties);
            message = new MimeMessage(session);
            doSendHtmlEmail(subject, sendHtml, receiveUser);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("发送邮件失败！");
            return  R.error(1,"发送邮件失败");
        }
        return R.ok(sendHtml);
    }
    public static void receiveEmail(String mailFro, String mailSmtp, String password) {
            mailFrom = mailFro;
            mailSmtpServer = mailSmtp;
            mailPassword = password;
    }
    /**
     * 发送邮件
     *
     * @param subject     邮件主题
     * @param htmlContent    邮件内容
     * @param receiveUser 收件人地址
     */
    private static void doSendHtmlEmail(String subject, String htmlContent, String receiveUser) throws javax.mail.MessagingException {
        try {
            Properties props = new Properties();
            // 开启debug调试
            props.setProperty("mail.debug", "true");
            // 发送服务器需要身份验证
            props.setProperty("mail.smtp.auth", "true");
            // 设置邮件服务器主机名
            props.setProperty("mail.host", mailSmtpServer);
            // 发送邮件协议名称
            props.setProperty("mail.transport.protocol", "smtp");
            props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.setProperty("mail.smtp.port", "465");
            props.setProperty("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.starttls.enable", "true");

            // 设置环境信息
            Session localSession = Session.getInstance(props, new javax.mail.Authenticator() {
                // 在session中设置账户信息，Transport发送邮件时会使用
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(mailFrom, mailPassword);
                }
            });

            // 创建邮件对象
            Message msg = new MimeMessage(localSession);
            // 发件人
            msg.setFrom(new InternetAddress(mailFrom));
            // 多个收件人
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receiveUser));
            // 抄送人
            //msg.setRecipient(Message.RecipientType.CC, new InternetAddress("java_mail_001@163.com"));
            // 暗送人
            //msg.setRecipient(Message.RecipientType.BCC, new InternetAddress("java_mail_004@163.com"));

            // 主题
            msg.setSubject(subject);
            // HTML内容
            msg.setContent(htmlContent, "text/html;charset=utf-8");
            Transport.send(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

